<template>
  <div class="jsbos-common-layout taskMsgs">
    <div class="jsbos-common-layout-center">
      <el-row class="jsbos-common-search-box" :gutter="18">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item label="关键字">
              <el-input
                v-model="listQuery.keyword"
                placeholder="编号、业务系统"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="发送配置">
              <MsgSendDialog
                v-model="query.sendCode"
                :title="query.sendName"
                @change="onMsgSendChange"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="接收者">
              <user-select
                v-model="query.toUserIds"
                placeholder="请选择"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="search()"
                >查询</el-button
              >
              <el-button icon="el-icon-refresh-right" @click="reset()"
                >重置</el-button
              >
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="jsbos-common-layout-main jsbos-flex-main">
        <div class="jsbos-common-head">
          <div>
            <!-- v-has="'btn_add'" -->
            <el-button
              :disabled="activeName != 1"
              type="primary"
              icon="el-icon-plus"
              @click="addOrUpdateHandle()"
              >新增
            </el-button>
          </div>
          <div class="jsbos-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link
                icon="icon-ym icon-ym-Refresh jsbos-common-head-icon"
                :underline="false"
                @click="reset()"
              />
            </el-tooltip>
          </div>
        </div>
        <el-tabs v-model="activeName" type="card" @tab-click="handleTabClick">
          <el-tab-pane label="待发送" name="1"> </el-tab-pane>
          <el-tab-pane label="已发送" name="2"></el-tab-pane>
        </el-tabs>
        <jsbos-table
          v-loading="listLoading"
          :data="dataList"
          :border="true"
          :hasNO="false"
          style="margin-left: 5px"
          @sort-change="sortChange"
        >
          <el-table-column
            prop="id"
            label="编号"
            width="150"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="taskCode"
            label="业务系统"
            width="100"
            :align="'center'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="sendCode"
            label="发送配置"
            min-width="150"
            :align="'left'"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              {{ scope.row.sendCode + "(" + scope.row.sendName + ")" }}
            </template>
          </el-table-column>
          <el-table-column
            prop="toUserIds"
            label="接收者"
            min-width="150"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="toDeptIds"
            label="接收者的部门"
            width="120"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="businessKey"
            label="关键参数"
            min-width="300"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="customKey"
            label="自定义参数"
            min-width="350"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="enabledMark"
            label="状态"
            width="70"
            algin="'center'"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <el-tag
                :type="scope.row.enabledMark == 2 ? 'success' : 'warning'"
                disable-transitions
              >
                {{ scope.row.enabledMark | dynamicText(enabledMarkOptions) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="creatorUserId"
            label="创建人"
            width="100"
            :align="'center'"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="creatorTime"
            label="创建时间"
            width="180"
            :align="'center'"
            sortable="custom"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="description"
            label="描述"
            width="100"
            :align="'left'"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="操作" fixed="right" width="160">
            <template slot-scope="scope">
              <!-- v-has="'btn_edit'" -->
              <el-button
                v-if="scope.row.enabledMark == 1"
                type="text"
                @click="addOrUpdateHandle(scope.row.id)"
                >编辑
              </el-button>
              <!-- v-has="'btn_remove'" -->
              <el-button
                v-if="scope.row.enabledMark == 1"
                type="text"
                class="jsbos-table-delBtn"
                @click="handleDel(scope.row.id)"
                >删除
              </el-button>
              <!-- v-has="'btn_detail'" -->
              <el-button type="text" @click="goDetail(scope.row.id)"
                >详情
              </el-button>
              <!-- v-has="'btn_send'" -->
              <el-button
                v-if="scope.row.enabledMark == 1"
                type="text"
                @click="sendHandle(scope.row.id)"
                >发送
              </el-button>
              <!-- v-has="'btn_send'" -->
              <el-button
                v-if="scope.row.enabledMark == 2"
                type="text"
                @click="receiveDialog(scope.row.id)"
                >记录
              </el-button>
            </template>
          </el-table-column>
        </jsbos-table>
        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="initData"
        />
      </div>
    </div>
    <Form v-if="formVisible" ref="Form" @refresh="refresh"></Form>
    <Detail
      v-if="detailVisible"
      ref="Detail"
      @refresh="detailVisible = false"
    />
    <receive-dialog
      v-show="receiveDialogVisible"
      ref="receiveDialogVisibleRef"
      @close="closeReceiveDialog"
      :menuId="menuId"
    />
  </div>
</template>

<script>
import { getList, Delete, send } from "@/views/message/api/mcTaskMsg";
import Form from "./Form";
import Detail from "./Detail";
import MsgSendDialog from "./msgSendDialog";
import receiveDialog from "./receiveDialog";

export default {
  name: "mcTaskMsg",
  components: { Form, Detail, MsgSendDialog, receiveDialog },
  data() {
    return {
      activeName: "1",
      detailVisible: false,
      query: {
        taskCode: undefined,
        sendCode: undefined,
        toUserIds: undefined,
        enabledMark: undefined,
      },
      dataList: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        keyword: undefined,
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "creatorTime",
      },
      formVisible: false,
      columnList: [
        { prop: "taskCode", label: "业务系统" },
        { prop: "sendCode", label: "发送配置" },
        { prop: "businessKey", label: "场景关键词" },
        { prop: "toUserIds", label: "接收者" },
        { prop: "toDeptIds", label: "接收者的部门" },
        { prop: "customKey", label: "自定义参数" },
        { prop: "enabledMark", label: "状态" },
        { prop: "description", label: "描述" },
        { prop: "creatorTime", label: "创建时间" },
        { prop: "creatorUserId", label: "创建人" },
        { prop: "lastModifyTime", label: "修改时间" },
        { prop: "lastModifyUserId", label: "修改人" },
      ],
      enabledMarkOptions: [
        { fullName: "待发送", id: "1" },
        { fullName: "已发送", id: "2" },
      ],
      enabledMarkProps: { label: "fullName", value: "id" },
      receiveDialogVisible: false,
    };
  },
  computed: {
    menuId() {
      return this.$route.meta.modelId || "";
    },
  },
  created() {
    this.initData();
  },
  methods: {
    goDetail(id) {
      this.detailVisible = true;
      this.$nextTick(() => {
        this.$refs.Detail.init(id);
      });
    },
    sortChange({ column, prop, order }) {
      this.listQuery.sort = order == "ascending" ? "asc" : "desc";
      this.listQuery.sidx = !order ? "" : prop;
      this.initData();
    },
    initData() {
      const activeId = this.activeName;
      this.query.enabledMark = activeId;
      this.listLoading = true;
      let _query = {
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
      };
      getList(_query).then((res) => {
        var _list = [];
        for (let i = 0; i < res.data.list.length; i++) {
          let _data = res.data.list[i];
          _list.push(_data);
        }
        this.dataList = _list;
        this.total = res.data.pagination.total;

        this.listLoading = false;
      });
    },
    handleTabClick() {
      this.initData();
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          Delete(id).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    handleSelectionChange(val) {
      const res = val.map((item) => item.id);
      this.multipleSelection = res;
    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.Form.init(id, isDetail);
      });
    },
    // 发送
    sendHandle(id) {
      this.$confirm("确定下发消息吗?", "提示", {
        type: "warning",
      })
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: "正在发送中，请稍等...",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)",
          });
          send(id).then((res) => {
            loading.close();
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
                onClose: () => {
                  this.initData();
                },
              });
            } else {
              this.$message({
                type: "error",
                message: res.msg,
              });
              return;
            }
          });
        })
        .catch(() => {});
    },
    // 下发记录
    receiveDialog(id) {
      this.receiveDialogVisible = true;
      this.$nextTick(() => {
        this.$refs.receiveDialogVisibleRef.init(id);
      });
    },
    closeReceiveDialog(isRefresh) {
      this.receiveDialogVisible = false;
      if (isRefresh) this.initData();
    },
    search() {
      this.initData();
    },
    refresh(isInitData) {
      this.formVisible = false;
      if (isInitData) this.initData();
    },
    reset() {
      this.resetQuery();
      this.resetListQuery();
      this.initData();
    },
    resetQuery() {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
    },
    resetListQuery() {
      this.listQuery = {
        keyword: "",
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "creatorTime",
      };
    },
    /**消息模板change事件 */
    onMsgSendChange(enCode, item) {
      if (!enCode) return (this.query.sendName = "");
      this.query.sendCode = enCode;
      this.query.sendName = item.fullName;
    },
  },
};
</script>

<style lang="scss" scoped>
.taskMsgs {
  common-layout-main {
    padding: 0;
  }
  .msgTabs {
    height: 100%;
  }
  .line1 {
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    word-break: break-all;
  }
  >>> .el-tabs__content {
    height: calc(100% - 40px);
    padding: 0;
    .el-tab-pane {
      height: calc(100% - 110px);
      overflow: hidden;
    }
  }
}
</style>
